package com.imy.luascript.base.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.imy.luascript.base.activity.BaseActivity;
import com.imy.luascript.config.GlideApp;
import com.imy.luascript.utils.RxUtils;
import com.imy.luascript.utils.ViewControl;
import com.imy.luascript.viewmodel.BaseImplView;
import com.imy.luascript.viewmodel.BaseViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.subscriptions.CompositeSubscription;

/**
 * @ProjectName: frame
 * @Package: com.imy.frame.base.fragment
 * @ClassName: BaseFragment
 * @Description: 描述
 * @Author: 蒋明伟
 * @CreateDate: 2020/10/30 16:54
 */
public abstract class BaseFragment<V extends BaseViewModel> extends Fragment implements BaseImplView {

    protected ViewControl viewControl;
    protected V mViewModel;
    private Unbinder unbinder;
    protected CompositeSubscription mCompositeSubscription;
    protected Activity self;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        self = getActivity();
        Bundle arguments = getArguments();
        if (arguments != null) {
            getBundleExtras(arguments);
        }
        if (isBindEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    protected void onRxEventSubscriber() {

    }

    /**
     * 是否需要绑定EventBus
     */
    protected boolean isBindEventBus() {
        return false;
    }

    protected void getBundleExtras(Bundle arguments) {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewControl = new ViewControl(null);
        View contentView = viewControl.getContentView(getActivity(), getLayoutID());
        unbinder = ButterKnife.bind(this, contentView);
        mViewModel = getViewModel();
        initView(contentView);
        return contentView;
    }

    protected void showNoData(String msg) {
        viewControl.showNoData(msg);
    }

    public void showNoData(String msg, int res, View.OnClickListener onClickListener){
        viewControl.showNoData(msg,res,onClickListener);
    }
    public void hideNoData(){
        viewControl.hideNoData();
    }
    protected void initView(View contentView) {

    }

    protected abstract V getViewModel();

    @Override
    public void onPause() {
        super.onPause();
        GlideApp.with(this).pauseRequests();
    }

    @Override
    public void onResume() {
        super.onResume();
        GlideApp.with(this).resumeRequests();
    }

    @Override
    public void onDestroy() {
        if (getActivity() != null) {
            GlideApp.get(getActivity()).clearMemory();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();
            mCompositeSubscription = null;
        }
        //清空viewmodel的缓存
        if (mViewModel != null) {
            mViewModel.onCleared();
        }
        if (isBindEventBus()){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    protected abstract int getLayoutID();
}